package model;

import entities.User;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

public class ViewUserTableModel extends AbstractTableModel {

    ArrayList<User> list;

    public ViewUserTableModel() {
        list = new ArrayList<User>();
    }

    public ViewUserTableModel(ArrayList<User> list) {
        this.list = list;
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return 10;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        User user = list.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return user.getLoginID();
            case 1:
                return user.getName();
            case 2:
                return user.getRole();
            case 3:
                return user.getLevel();
            case 4:
                return user.isMale();
            case 5:
                return user.getBirthDate();
            case 6:
                return user.getAddress();
            case 7:
                return user.getPhone();
            case 8:
                return user.getEmailAddress();
            case 9:
                return user.getHireDate();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return Integer.class;
            case 3:
                return Integer.class;
            case 4:
                return Boolean.class;
            case 5:
                return Date.class;
            case 6:
                return String.class;
            case 7:
                return String.class;
            case 8:
                return String.class;
            case 9:
                return Date.class;
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Role";
            case 3:
                return "Level";
            case 4:
                return "Gender";
            case 5:
                return "BirthDate";
            case 6:
                return "Address";
            case 7:
                return "Phone";
            case 8:
                return "Email Address";
            case 9:
                return "Hire Date";
            default:
                return null;
        }
    }

    public boolean addAll(ArrayList<User> list) {
        boolean returnVal = this.list.addAll(list);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public boolean addElement(User user) {
        boolean returnVal = list.add(user);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public User getAt(int index) {
        return list.get(index);
    }

    public void clear() {
        list.clear();
    }
}
